package Frame;
import DataBase.SQL_Readin;
import InterFace.NorWin_api;
import MeHua.NewNorPanel;
import MeHua.UBorder;
import MeHua.UFONT;
import com.eltima.components.ui.DatePicker;
import pojo.MessageBoard;
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;
import java.sql.Statement;
import java.util.ArrayList;
import static pojo.NewTable.utable;

public class NorWin implements NorWin_api {//郭
    SQL_Readin db = new SQL_Readin();
    JLabel number =new JLabel();
    JLabel class_id=new JLabel();
    String className;
    //本窗口
    JLabel tip = new JLabel("请在上方菜单选择功能");
    JFrame BaseWin = new JFrame("普通用户");
    JButton b_inf_nor = new JButton("查看班级记录");
    JButton b_message_nor = new JButton("班级留言板");
    JMenuBar menuBar=new JMenuBar();
    JMenu menu_inf_nor =new JMenu("查看班级记录");
    JMenu menu_message_nor=new JMenu("班级留言板");
    JMenuItem menu_money =new JMenuItem("日常消费记录");
    JMenuItem menu_action=new JMenuItem("班级活动记录");
    JMenuItem menu_RePun=new JMenuItem("奖惩记录");
    JMenuItem menu_message=new JMenuItem("班级留言薄");

    //记录查看左面板
    JPanel inf_panel = new JPanel();
    JButton b_money_nor = new JButton("日常消费记录");
    JButton b_action_nor = new JButton("班级活动记录");
    JButton b_RePun_nor = new JButton("奖惩记录");
    //留言信息
    NewNorPanel message_panel = new NewNorPanel();
    JPanel message_panel_nor = new JPanel();
    JButton b_mess_nor = new JButton("班级留言簿");
    Connection conn;
    Statement stat;
    ResultSet rs;
    ArrayList<String> list;
    String url="";
    String user="";
    String password="";
    JTextArea textArea;
    JButton Add;
    JPanel money_panel = new JPanel();
    JPanel action_panel = new JPanel();
    JPanel RePun_panel = new JPanel();
    //左面板导航栏
    JLabel map_first_nor = new JLabel("请在上方菜单选择功能");
    JLabel map_second_mess = new JLabel("==>留言查看功能");
    JLabel map_thread_mess = new JLabel(" ");
    JLabel map_second_inf = new JLabel("==>记录方面查看功能");
    JLabel map_thread_inf = new JLabel(" ");
    //下级导航
    JLabel map_next_money = new JLabel(">班级消费记录");
    JLabel map_next_RePun = new JLabel(">班级奖惩记录 ");
    JLabel map_next_action = new JLabel(">班级活动记录");
    JLabel map_next_mess = new JLabel(">班级留言记录");
    public NorWin(String number,String id){
        this.number.setText("普通用户:  "+number);
        this.class_id.setText("班级:  "+id);
        this.className = id;
    }
    public void creat(){//郭
        BaseWin.add(tip);
        tip.setBounds(20,50,200,50);
        BaseWin.setBounds(400,100,1000,700);
        BaseWin.setLayout(null);
        BaseWin.setResizable(false);
        BaseWin.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        BaseWin.add(number);
        BaseWin.add(class_id);
        class_id.setBounds(700,0,150,50);
        number.setBounds(850,0,150,50);
        class_id.setFont(UFONT.getFont3());
        number.setFont(UFONT.getFont3());
        number.setForeground(new Color(95,95,95));
        class_id.setForeground(new Color(95,95,95));
        menuBar.setPreferredSize(new Dimension(1000,50));
        menuBar.add(menu_inf_nor);
        menuBar.add(menu_message_nor);
        menu_message_nor.add(menu_message);
        menu_inf_nor.add(menu_RePun);
        menu_inf_nor.add(menu_action);
        menu_inf_nor.add(menu_money);
        BaseWin.setJMenuBar(menuBar);
        BaseWin.add(inf_panel);
        inf_panel.setBounds(10,100,200,600);
        BaseWin.add(message_panel);
        map_first_nor.setFont(UFONT.getFont3());
        map_second_mess.setFont(UFONT.getFont3());
        map_thread_mess.setFont(UFONT.getFont3());
        map_second_inf.setFont(UFONT.getFont3());
        map_thread_inf.setFont(UFONT.getFont3());
        message_panel.setBounds(10,100,200,600);
        inf_panel.setBackground(new Color(244, 255, 255));
        message_panel.setBackground(new Color(244, 255, 255));
        BaseWin.add(message_panel);
        message_panel.setBounds(210,100,800,600);
        BaseWin.setVisible(true);
        menu_inf_nor.addMenuListener(new MenuListener(){
            public void menuSelected(MenuEvent e) {
                BaseWin.remove(inf_panel);
                BaseWin.remove(message_panel);
                inf_panel.remove(map_next_money);
                inf_panel.remove(map_next_RePun);
                inf_panel.remove(map_next_action);
                fun_inf_panel();
            }

            public void menuDeselected(MenuEvent e) {

            }

            public void menuCanceled(MenuEvent e) {

            }
        });
        menu_message_nor.addMenuListener(new MenuListener() {
            public void menuSelected(MenuEvent e) {
                BaseWin.remove(inf_panel);
                BaseWin.remove(message_panel);
                message_panel.remove(map_next_mess);
                fun_message_panel();
            }

            @Override
            public void menuDeselected(MenuEvent e) {

            }

            @Override
            public void menuCanceled(MenuEvent e) {

            }
        });

    }
    void fun_inf_panel(){//王
        BaseWin.add(inf_panel);
        BaseWin.repaint();
        inf_panel.setLayout(null);
        inf_panel.setBounds(10,100,200,600);
        inf_panel.add(map_first_nor);
        inf_panel.add(map_second_inf);
        inf_panel.add(map_thread_inf);
        map_first_nor.setBounds(10,50,150,30);
        map_second_inf.setBounds(40,100,150,30);
        map_thread_inf.setBounds(70,150,150,30);
        //次级
        map_second_inf.setBorder(UBorder.getBorder1());
        map_next_money.setBorder(UBorder.getBorder1());
        map_next_RePun.setBorder(UBorder.getBorder1());
        map_next_action.setBorder(UBorder.getBorder1());
        map_second_inf.setForeground(new Color(58,95,205));
        map_next_money.setForeground(new Color(58,95,205));
        map_next_action.setForeground(new Color(58,95,205));
        map_next_RePun.setForeground(new Color(58,95,205));
        map_next_money.setBounds(70,150,150,30);
        map_next_RePun.setBounds(70,190,150,30);
        map_next_action.setBounds(70,230,150,30);
        //导航菜单
        map_second_inf.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                inf_panel.remove(map_thread_inf);
                inf_panel.add(map_next_money);
                inf_panel.add(map_next_action);
                inf_panel.add(map_next_RePun);
                inf_panel.repaint();
            }
        });
        map_next_money.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                fun_money();
            }
        });
        map_next_action.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                fun_action();
            }
        });
        map_next_RePun.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                fun_RePun();
            }
        });
        //日常消费记录
        menu_money.addActionListener(e -> {
            fun_money();
            map_thread_inf.setText("=>消费记录查看");
        });
        //班级活动
        menu_action.addActionListener(e -> {
            fun_action();
            map_thread_inf.setText("=>班级活动记录查看");
        });
        //奖惩记录
        menu_RePun.addActionListener(e -> {
            fun_RePun();
            map_thread_inf.setText("=>班级奖惩记录查看");
        });
    }
    void fun_message_panel(){//王
        BaseWin.add(message_panel);
        BaseWin.repaint();
        message_panel.setLayout(null);
        message_panel.setBounds(10,100,200,600);
        //message_panel.add(b_mess_nor);
        //b_mess_nor.setBounds(0,10,100,60);
        message_panel.add(map_first_nor);
        message_panel.add(map_second_mess);
        message_panel.add(map_thread_mess);
        map_first_nor.setBounds(10,50,150,30);
        map_second_mess.setBounds(40,100,150,30);
        map_thread_mess.setBounds(70,150,150,30);
        map_next_mess.setBounds(70,150,150,30);
        //导航
        map_second_mess.setBorder(UBorder.getBorder1());
        map_next_mess.setBorder(UBorder.getBorder1());
        map_second_mess.setForeground(new Color(58,95,205));
        map_next_mess.setForeground(new Color(58,95,205));
        map_second_mess.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                message_panel.remove(map_thread_mess);
                message_panel.add(map_next_mess);
                message_panel.repaint();
            }
        });
        map_next_mess.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                fun_mess();
            }
        });
        menu_message.addActionListener(e -> {
            fun_mess();
            map_thread_mess.setText("=>留言簿");
        });
    }
    //日常消费记录
    void fun_money(){//王
        money_panel = flush();
        JScrollPane scrollPane =new JScrollPane();
        JList<String> list = utable.creat_table_money("readMoney",className);
        BaseWin.add(money_panel);
        money_panel.setLayout(null);
        money_panel.setBounds(210,100,800,600);
        BaseWin.repaint();
        String[] c_name={"序号","收入/支出","金额","日期","来源/去向"};
        JTable table = creatTable(list,c_name);
        creatTableSCP(scrollPane,table,money_panel);
        money_panel.add(scrollPane);
    }
    //班级活动记录
    void fun_action(){//王
        action_panel = flush();
        JScrollPane scrollPane =new JScrollPane();
        JList<String> list = utable.creat_table_peo("readAction",className);
        BaseWin.add(action_panel);
        action_panel.setLayout(null);
        action_panel.setBounds(210,100,800,600);
        BaseWin.repaint();
        String[] c_name={"序号","活动名称","活动日期","主办方","活动地点","活动人数","活动内容"};
        JTable table = creatTable(list,c_name);
        creatTableSCP(scrollPane,table,action_panel);
        action_panel.add(scrollPane);
    }
    //奖惩记录
    void fun_RePun(){//王
        BaseWin.remove(RePun_panel);
        RePun_panel = flush();
        JScrollPane scrollPane =new JScrollPane();
        JList<String> list = utable.creat_table_peo("readRePun", className);
        BaseWin.add(RePun_panel);
        RePun_panel.setLayout(null);
        RePun_panel.setBounds(210,100,800,600);
        BaseWin.repaint();
        String[] c_name={"序号","学号","姓名","处分/奖励","理由","日期"};
        JTable table = creatTable(list,c_name);
        creatTableSCP(scrollPane,table,RePun_panel);
        JButton RePun_add=new JButton("增加记录");
        JButton RePun_delete=new JButton("删除记录");
        RePun_panel.add(scrollPane);
    }
    //班级留言
    void fun_mess(){//王
        message_panel_nor = flush();
        JScrollPane scrollPane =new JScrollPane();
        JList<String> list = utable.creat_table_message("readMessage",className);
        BaseWin.add(message_panel_nor);
        message_panel_nor.setLayout(null);
        message_panel_nor.setBounds(210,100,800,600);
        BaseWin.repaint();
        String[] c_name={"序号","学号","姓名","留言内容","日期"};
        JTable table = creatTable(list,c_name);
        creatTableSCP(scrollPane,table,message_panel_nor);
        message_panel_nor.add(scrollPane);
        //增加留言；
        //增加记录留言
        JButton message_add =new JButton("增加留言");
        message_add.setFont(UFONT.getFont4());
        message_panel_nor.add(message_add);
        message_add.setBounds(600,400,150,60);
        int[] flag_id = new int[1];
        table.getSelectionModel().addListSelectionListener(e1 -> {
            if(table.getSelectedRow()!=-1){
                flag_id[0]=table.getSelectedRow();
            }
        });
        message_add.addActionListener(e -> {
            JFrame frame = new JFrame("录入留言");
            //la1=new JLabel("序号:");
            //la2=new JLabel("学号:");
            JLabel la3=new JLabel("姓名:");
            JLabel la4=new JLabel("留言内容:");
            JLabel la5=new JLabel("留言时间:");
            //ta1=new JTextField();
            //ta2=new JTextField();
            JTextField ta3=new JTextField();
            JTextField  ta4=new JTextField();
            //JTextField ta5=new JTextField();
            frame.setBounds(600,200,400,450);
            frame.setLayout(null);
            JButton b_ok = new JButton("确定");
            frame.add(b_ok);
            b_ok.setBounds(50,350,100,50);
            set_Message_Bounds(frame,la3,la4,la5,ta3,ta4);
            DatePicker dp = new DatePicker();
            frame.add(dp);
            dp.setBounds(50,200,250,50);
            b_ok.addActionListener(e1 -> {
                MessageBoard messageBoard= new MessageBoard();
                //messageBoard.setMessageId(Integer.parseInt(ta1.getText()));
                //messageBoard.setStudentId(Integer.parseInt(ta2.getText()));
                messageBoard.setStudentName(ta3.getText());
                messageBoard.setMessageContent(ta4.getText());
                messageBoard.setMessageDate(DareSelecter.ReDate(dp));
                messageBoard.setClassName(className);

                try {
                    if(db.addMessageBoard(messageBoard)){
                        JOptionPane.showMessageDialog(null,"增加成功");
                        frame.dispose();
                        BaseWin.remove(message_panel_nor);
                        BaseWin.repaint();
                        message_panel_nor.repaint();
                        fun_mess();
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "增加失败");
                    }
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(null, ex);
                }
            });
            JButton b_cancel = new JButton("取消");
            frame.add(b_cancel);
            b_cancel.setBounds(200,350,100,50);
            b_cancel.addActionListener(e3 -> {
                frame.dispose();
            });
            frame.setResizable(false);
            frame.setVisible(true);
        });

    }
    private void set_Message_Bounds(JFrame frame,JLabel l3,JLabel l4,JLabel l5,JTextField tf3,JTextField tf4) {
        frame.add(l3);
        l3.setBounds(0,100,50,50);
        frame.add(l4);
        l4.setBounds(0,150,50,50);
        frame.add(l5);
        l5.setBounds(0,200,50,50);
        frame.add(tf3);
        tf3.setBounds(50,100,250,50);
        frame.add(tf4);
        tf4.setBounds(50,150,250,50);

    }

    private NewNorPanel flush(){//郭
        BaseWin.remove(money_panel);
        BaseWin.remove(message_panel_nor);
        BaseWin.remove(action_panel);
        BaseWin.remove(RePun_panel);
        RePun_panel.removeAll();
        message_panel_nor.removeAll();
        money_panel.removeAll();
        action_panel.removeAll();
        return new NewNorPanel();
    }
    private JTable creatTable(JList<String> list,String[] c_name){
        String[][] str=new String[list.getModel().getSize()][c_name.length];
        for(int i=0;i<list.getModel().getSize();i++) {
            String[] temp= list.getModel().getElementAt(i).split(" , ");
            for(int j=0;j<c_name.length;j++) {
                str[i][j]=temp[j];
            }
        }
        return new JTable(str,c_name);
    }
    private void creatTableSCP(JScrollPane scrollPane, JTable table, JPanel unitPanel){
        BaseWin.add(unitPanel);
        unitPanel.setLayout(null);
        unitPanel.setBounds(210,100,800,600);
        BaseWin.repaint();
        unitPanel.add(scrollPane);
        scrollPane.setBounds(0,0,550,500);
        scrollPane.getViewport().add(table,null);
    }
}
